package com.design.adnroid.campussecondarytrading.activity;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.design.adnroid.campussecondarytrading.AppContext;
import com.design.adnroid.campussecondarytrading.database.DBOperate;
import com.design.adnroid.campussecondarytrading.entity.Goods;

import java.text.SimpleDateFormat;
import java.util.Date;


public class AddGoodsActivity extends AppCompatActivity {

    private EditText etGoodsName, etGoodsText, etGoodsPrice, etGoodsOwnerContact;
    private ImageView ivGoodsImage;

    private DBOperate dbOperate;

    private String loadImagePath = "";
    private static final int RESULT_LOAD_IMAGE = 10101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_goods);
        setTitle(getString(R.string.title_activity_addGoods));

        etGoodsName = findViewById(R.id.add_goodsName);
        etGoodsText = findViewById(R.id.add_goodsText);
        etGoodsPrice = findViewById(R.id.add_goodsPrice);
        etGoodsOwnerContact = findViewById(R.id.add_goodsOwnerContact);

        ivGoodsImage = findViewById(R.id.add_goodsImage);
        Button btGoodsSureAdd = findViewById(R.id.add_goodsSureAdd);

        dbOperate = new DBOperate(this);

        btGoodsSureAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addGoods();
            }
        });

        ivGoodsImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //一个重定向打开系统图库
                Intent i = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        ivGoodsImage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                loadImagePath = "";
                ivGoodsImage.setImageDrawable(getDrawable(R.mipmap.add));
                return true;
            }
        });
    }//onCreate

    //获取本地图片路径并加载显示图片
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            try {
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                loadImagePath = cursor.getString(columnIndex);
                cursor.close();

                //从手机选择图片显示到ivGoodsImage
                //                System.out.println("加载图片路径:"+picturePath);
                ivGoodsImage.setImageBitmap(BitmapFactory.decodeFile(loadImagePath));
                //                Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
                //                ivGoodsImage.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void addGoods() {
        String goodsName = etGoodsName.getText().toString();
        String goodsText = etGoodsText.getText().toString();
        String goodsPriceString = etGoodsPrice.getText().toString();
        double goodsPrice;
        String goodsOwnerContact = etGoodsOwnerContact.getText().toString();

        if (goodsName.isEmpty()) {
            etGoodsName.requestFocus();
            etGoodsName.setError("null1");
            return;
        }
        if (goodsText.isEmpty()) {
            etGoodsText.requestFocus();
            etGoodsText.setError("null2");
            return;
        }
        if (goodsPriceString.isEmpty()) {
            etGoodsPrice.requestFocus();
            etGoodsPrice.setError("null3");
            return;
        }
        if (goodsOwnerContact.isEmpty()) {
            etGoodsOwnerContact.requestFocus();
            etGoodsOwnerContact.setError("null3");
            return;
        }
        if (loadImagePath.isEmpty()) {
            Toast.makeText(getApplicationContext(), "图片为空！", Toast.LENGTH_SHORT).show();
            ivGoodsImage.requestFocus();
            return;
        }
        goodsPrice = Double.valueOf(goodsPriceString);

        long time = System.currentTimeMillis();
        String goodsTime = formatterTime(time);
        String goodsId = AppContext.getUserMd5() + time;

        Bitmap goodsImage = BitmapFactory.decodeFile(loadImagePath);
        String goodsOwner = AppContext.getUserName();
        int goodsState = 0;
        String goodsBuyers = new String();

        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setGoodsName(goodsName);
        goods.setGoodsText(goodsText);
        goods.setGoodsImage(goodsImage);
        goods.setGoodsTime(goodsTime);
        goods.setGoodsPrice(goodsPrice);
        goods.setGoodsOwner(goodsOwner);
        goods.setGoodsOwnerContact(goodsOwnerContact);
        goods.setGoodsState(goodsState);
        goods.setGoodsBuyers(goodsBuyers);

        dbOperate.openDB();
        dbOperate.addGoods(goods);
        dbOperate.closeDb();

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    public String formatterTime(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date curDate = new Date(time);
        return formatter.format(curDate);
    }

}
